import Axios from "axios";

const state = {     //管理状态
    CustomerPermissionRow: [],      //客户查看权限数据
    allTreeData: [],                //查看所有客户数据
    treeData:[],                    //树形结构数据
    depName:[],                     //部门名称 
    rowId:'',                       //修改行id
    newdeptId:'',                   //当前选中树节点的id  
    customerSelectRowId: '',                     //表格选中当前行的customerSelectRowId
    searchData:[],
    userId: '',//用户id
    userName:'',//用户名
    name: '',//名称
    selectRowId: '',
    expandNodeId: null,       //要高亮展开的id
    documentNodeId: null,
    tableData: [],
    parentId: [],		//上级部门id
    lastDepartmentName: [],		//上级部门名称
    tables: [],
    documentsTreeData: [],  //单据的树形数据
    documentsTableData:[],//单据的表格数据
    loading: [true],
}

const mutations = {
    updateLoading(state, bool) {
        state.loading.splice(0, 1, bool);
    },
    updateExpandNodeId(state, id) {         //更新要展开高亮的id
		state.expandNodeId = id ? id : null;
    },
    updateDocumentNodeId(state, id){
        state.documentNodeId = id ? id : null;
    },
    updateTable(state, arr) {			//更新表格数据
    	let tableData = state.tableData;
    	tableData.length = 0;
    	if(arr.length > 0) {
    		arr.forEach(item => tableData.push(item));
    	} else {
    		tableData.splice(0);
    	}
    },
    updateParentId(state, id) {
    	state.parentId.splice(0, 1, id);
    },
    updateCustomerPermission(state, arr) {      //更新客户查看权限状态
        let CustomerPermissionRow = state.CustomerPermissionRow;
        CustomerPermissionRow.length = 0;           //清空数组
        arr.forEach(item => {
            if(item != null){
                CustomerPermissionRow.push(item)
            }
        });
    },
    updateCustomerPer(state, obj) {             //新增客户查看权限
        let CustomerPermissionRow = state.CustomerPermissionRow;
        let flag = true;        //是否可以添加
        CustomerPermissionRow.forEach((item) => {
            if(item['id'] == obj['id']) {
                flag = false;
                return false;
            }
        });
        if(flag) {
            CustomerPermissionRow.push(obj);
        }
    },
    CustomerData(state, arr) {                  //更新客户查看权限状态
        let allTreeData = state.allTreeData;
        allTreeData.length = 0;           //清空数组
        arr.forEach(item => allTreeData.push(item));
    },
    updateRowId(state,id){                      //修改行的id
        state.rowId = id ? id : null;
    },
    updateCustomerSelectRowId(state,id){                     //表格选中当前行的customerSelectRowId
        state.customerSelectRowId = id;
    },
    setContext(state,data){                 //查询按钮
        let searchData = state.searchData;
        if(data.length > 0) {
            searchData.length = 0;
            data.forEach(item => searchData.push(item));
        } else {
            searchData.splice(0);
        }
    },
    updateUserId(state, res){
        state.userId = res;
    },
    updateUserName(state, res){
        state.userName = res;
    },
    updateName(state, res){
        state.name = res;
    },
    updateSelectRowId(state, res){
        state.selectRowId = res;
    },
    setDocumentsTreeData(state, Arr){
        let documentsTreeData = state.documentsTreeData;
        documentsTreeData.length = 0;
        if(Arr.length > 0){
            Arr.forEach(item => documentsTreeData.push(item));
        }else{
            documentsTreeData.splice(0);
        }
    },
    setDocumentsTableData(state, Arr){
        let documentsTableData = state.documentsTableData;
        documentsTableData.length = 0;
        if(Arr.length > 0){
            Arr.forEach(item => documentsTableData.push(item));
        }else{
            documentsTableData.splice(0);
        }
    },
}

const actions = {
    getCustomerPermission({ commit }, $url) {   //获取客户查看权限数据
        state.loading.splice(0, 1, true);
        Axios.get($url).then(res => {
            if(res.data.code == 20001) {
                state.loading.splice(0, 1, false);
                if(res.data.data) {
                    commit('updateCustomerPermission', res.data.data);
                }
            } else {
				state.loading.splice(0, 1, false);
			}
        });
    },
    getCustomerData({ commit }, $url) {         //获取所有客户数据
        Axios.get($url).then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    commit('CustomerData', res.data.data);
                }
            }
        });
    },
    getTreeData({state, commit, dispatch, rootState}) {				//获取部门数据
		//把公司数据拼接作为最顶级节点
		if(!state.expandNodeId) {
			state.expandNodeId = rootState.systemId;
		}
		let data = {
            name: rootState.companyName,
            id: rootState.systemId,
            level: 0,
            subDepartment: []
        }
        state.loading.splice(0, 1, true);
		Axios.get('department/selectObject?systemId=' + rootState.systemId).then(res => {
			if(res.data.code == 20001) {
                state.loading.splice(0, 1, false);
				if(res.data.data && res.data.data.length > 0) {
					data['subDepartment'] = res.data.data;		//将部门数据放到公司节点上
					state.treeData.splice(0, 1, data);
					state.parentId.splice(0, 1, rootState.systemId);        //更新上级部门id
                    state.lastDepartmentName.splice(0, 1, rootState.companyName);        //更新上级部门名称
				}
				if(state.expandNodeId) {
					dispatch('getEmployeeDataByDepartmentId', state.expandNodeId);
				} else {
					dispatch('getEmployeeDataByDepartmentId', 0);
				}
			} else {
				state.loading.splice(0, 1, false);
			}
		});
    },
    getEmployeeDataByDepartmentId({state, commit, rootState}, pid) {			//通过部门id找到对应的职员
		pid = pid == rootState.systemId ? 0 : pid;
		Axios.get('employee/findEmployeeByDepartment?id=' + pid + '&systemId=' + rootState.systemId).then(res => {
			if(res.data.code == 20001) {
				if(res.data.data) {
					commit('updateTable', res.data.data);
				}
			}
		});
	},
    getSearch({state}) {
		Axios.get('employee/selectEmployeeByName').then(res => {
			if(res.data.code == 20001) {
				if(res.data.data && res.data.data.length > 0) {
					let tables = state.tables,
						data = res.data.data;
						tables.length = 0;
					data.forEach(item => tables.push(item));
				}
			}
		});
    },
    getDocumentsTreeData({commit,dispatch},$url){
        Axios.get($url).then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    if(!state.documentNodeId) {
                        state.documentNodeId = res.data.data['submenuList'][0]['id'];
                    }
                    if(state.documentNodeId){
                        dispatch('getDocumentsTableData', state.documentNodeId);
                    }else{
                        dispatch('getDocumentsTableData', 0);
                    }
                    commit('setDocumentsTreeData', [res.data.data]);
                }
            }
        });
    },
    getDocumentsTableData({state, commit, rootState}, pid){
        Axios.post('userManage/selectFieldPermissionsByUserIdAndReceiptsId?receiptsId=' + pid + '&userId=' + state.userId).then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    commit('setDocumentsTableData', res.data.data);
                }
            }
        });
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}